package Heap;

import java.util.Comparator;
import java.util.Objects;
import java.util.PriorityQueue;
/**
 * P1337 矩阵中战斗力最弱的 K 行
 * */
public class P1337 {
    public static void main(String[] args) {

    }
}

class P1337Test01{

    private class T{
        int index;
        int sum;

        public T(int index, int sum) {
            this.index = index;
            this.sum = sum;
        }

        public int getIndex() {
            return index;
        }

        public void setIndex(int index) {
            this.index = index;
        }

        public int getSum() {
            return sum;
        }

        public void setSum(int sum) {
            this.sum = sum;
        }
    }
    /**
     * 自定义排序
     * */
    private class CompareT implements Comparator<T> {

        @Override
        public int compare(T o1, T o2) {
            if(o1.getSum()==o2.getSum())
                return o1.getIndex()-o2.getIndex();
            return o1.getSum()-o2.getSum();
        }
    }

    public int[] kWeakestRows(int[][] mat, int k) {
        PriorityQueue<T> queue=new PriorityQueue<>(new CompareT());
        int len=mat.length;

        for(int i=0;i<len;i++){
            int lenb=mat[i].length;
            int sum=0;
            for (int j=0;j<lenb&&mat[i][j]!=0;j++){
                sum++;
            }
            T t=new T(i, sum);
            queue.add(t);
        }

        int[] a=new int[k];

        for(int i=0;i<k;i++){
            a[i]= Objects.requireNonNull(queue.poll()).getIndex();
        }

        return a;
    }
}

